package com.lnlyj.media.data;

import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;

import static com.lnlyj.media.Constants.URI_MEDIA;

/**
 * Created by Wanglei on 2019/6/26.
 */

public class MediaItem implements Serializable, Parcelable {
    //new String[]{"_id", "_data", "_size", "date_modified", "latitude", "longitude", "width", "height"};
    private int id;
    private String path;
    private long size;
    private long dateModified;
    private long latitude;
    private long longitude;
    private int width;
    private int height;
    private boolean isTitle;
    private long bucketId;
    private String bucketDisplayName;
    private int count;
    private long duration;
    private int mediaType;

    public MediaItem(long dateModified) {
        this.dateModified = dateModified;
    }

    public MediaItem(Cursor cursor) {
        int index = cursor.getColumnIndex("_id");
        if (index != -1) {
            id = cursor.getInt(index);
        }

        index = cursor.getColumnIndex("_data");
        if (index != -1) {
            path = cursor.getString(index);
        }

        index = cursor.getColumnIndex("_size");
        if (index != -1) {
            size = cursor.getLong(index);
        }

        index = cursor.getColumnIndex("date_modified");
        if (index != -1) {
            dateModified = cursor.getLong(index);
        }

        index = cursor.getColumnIndex("latitude");
        if (index != -1) {
            latitude = cursor.getInt(index);
        }

        index = cursor.getColumnIndex("longitude");
        if (index != -1) {
            longitude = cursor.getInt(index);
        }

        index = cursor.getColumnIndex("width");
        if (index != -1) {
            width = cursor.getInt(index);
        }

        index = cursor.getColumnIndex("height");
        if (index != -1) {
            height = cursor.getInt(index);
        }

        index = cursor.getColumnIndex("bucket_id");
        if (index != -1) {
            bucketId = cursor.getLong(index);
        }

        index = cursor.getColumnIndex("bucket_display_name");
        if (index != -1) {
            bucketDisplayName = cursor.getString(index);
        }

        index = cursor.getColumnIndex("duration");
        if (index != -1) {
            duration = cursor.getLong(index);
        }

        index = cursor.getColumnIndex("media_type");
        if (index != -1) {
            mediaType = cursor.getInt(index);
        }
    }

    protected MediaItem(Parcel in) {
        id = in.readInt();
        path = in.readString();
        size = in.readLong();
        dateModified = in.readLong();
        latitude = in.readLong();
        longitude = in.readLong();
        width = in.readInt();
        height = in.readInt();
        isTitle = in.readByte() != 0;
        bucketId = in.readLong();
        bucketDisplayName = in.readString();
        count = in.readInt();
        duration = in.readLong();
        mediaType = in.readInt();
    }

    public static final Creator<MediaItem> CREATOR = new Creator<MediaItem>() {
        @Override
        public MediaItem createFromParcel(Parcel in) {
            return new MediaItem(in);
        }

        @Override
        public MediaItem[] newArray(int size) {
            return new MediaItem[size];
        }
    };

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getDateModified() {
        return dateModified;
    }

    public void setDateModified(long dateModified) {
        this.dateModified = dateModified;
    }

    public long getLatitude() {
        return latitude;
    }

    public void setLatitude(long latitude) {
        this.latitude = latitude;
    }

    public long getLongitude() {
        return longitude;
    }

    public void setLongitude(long longitude) {
        this.longitude = longitude;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public boolean isTitle() {
        return isTitle;
    }

    public void setTitle(boolean title) {
        isTitle = title;
    }

    public long getBucketId() {
        return bucketId;
    }

    public void setBucketId(long bucketId) {
        this.bucketId = bucketId;
    }

    public String getBucketDisplayName() {
        return bucketDisplayName;
    }

    public void setBucketDisplayName(String bucketDisplayName) {
        this.bucketDisplayName = bucketDisplayName;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public int getMediaType() {
        return mediaType;
    }

    public void setMediaType(int mediaType) {
        this.mediaType = mediaType;
    }

    public boolean isPhoto() {
        return mediaType == 1;
    }

    public boolean isVideo() {
        return mediaType == 3;
    }

    public Uri getUri() {
        return Uri.withAppendedPath(URI_MEDIA, String.valueOf(id));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MediaItem mediaItem = (MediaItem) o;

        if (bucketId == 0) {
            return path != null ? path.equals(mediaItem.path) : mediaItem.path == null;
        }

        return bucketId == mediaItem.bucketId;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(id);
        parcel.writeString(path);
        parcel.writeLong(size);
        parcel.writeLong(dateModified);
        parcel.writeLong(latitude);
        parcel.writeLong(longitude);
        parcel.writeInt(width);
        parcel.writeInt(height);
        parcel.writeByte((byte) (isTitle ? 1 : 0));
        parcel.writeLong(bucketId);
        parcel.writeString(bucketDisplayName);
        parcel.writeInt(count);
        parcel.writeLong(duration);
        parcel.writeInt(mediaType);
    }
}
